package com.jy.rhin.facade.ws.guahao;

import cn.org.chiss.rhin._2015.base.*;
import cn.org.chiss.rhin._2015.base.Integer;
import cn.org.chiss.rhin._2015.base.guahao.*;
import cn.org.chiss.rhin._2015.base.object.OrganizationBrief;
import cn.org.chiss.rhin._2015.base.object.PersonBrief;
import cn.org.chiss.rhin._2015.base.object.PractitionerBrief;
import com.jy.rhin.config.NamespaceConstants;
import com.jy.rhin.facade.ws.basic.wsEntry.exception.RhinWSException;
import com.jy.rhin.support.aspect.AuditLog;
import org.springframework.stereotype.Component;

import javax.jws.WebService;
import java.lang.String;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * This class was generated by Apache CXF 3.5.1
 * 2024-04-10T15:54:24.031+08:00
 * Generated source version: 3.5.1
 *
 */
@WebService(
        serviceName = "ScheduleAndReservation",
        portName = "ScheduleAndReservationPort",
        targetNamespace = NamespaceConstants.NS_RHIN,
        endpointInterface = "com.jy.rhin.facade.ws.guahao.ScheduleAndReservation")
@Component
public class ScheduleAndReservationPortImpl implements ScheduleAndReservation{

    @Override
    public void scheduleRevise(ScheduleListType scheduleList) {

    }

    @Override
    public Identifier reservationRequest(ReservationType reservation) {
        return null;
    }

    @Override
    @AuditLog
    public ReservationQueryResponseType reservationQuery(ReservationQueryType reservationQueryType) {
        if("1"
                .equals(reservationQueryType.getAdhocQuery().getSlot().get(0).getValueList().getValue().get(0).getValue())){
            //模拟反例
            throw new RhinWSException("ReservationResponseFault","查询异常");
        }
        ReservationQueryResponseType r = new ReservationQueryResponseType();
        ReservationListType reservationList = new ReservationListType();
        List<ReservationType> reservation = new ArrayList<>();
        ReservationType re = new ReservationType();
        Identifier d = new Identifier().setValue(new ID().setValue("171427159939687413")).setSystem(new URI().setValue("2.16.156.10011.12"));
        re.setIdentifier(d);
        List<ID> sliceid = new ArrayList<>();
        sliceid.add(new ID().setValue("3"));
        re.setScheduleRef(new ScheduleSliceRef().setIdentifier(d).setSliceID(sliceid));
        re.setPerson(getPatient());
        re.setAuthor(getDoctor());
        re.setDepartment(getReferralDepartment());
        re.setClassCode(new Code().setValue("1"));
        re.setDateTime(new DateTime().setValue("20240425190000"));
        reservation.add(re);
        reservationList.setReservation(reservation);
        r.setReservationList(reservationList);
        return r;
    }
    private OrganizationBrief getPartOf(){
        return new OrganizationBrief().setIdentifier(getIdentifier("440305009007","2.16.156.10011.8"))
                .setName(new cn.org.chiss.rhin._2015.base.String().setValue("深圳市南山区医疗集团总部大汪社区健康服务中心"))
                .setTelecom(new Telecom().setValue(new cn.org.chiss.rhin._2015.base.String().setValue("89095177")));
    }
    private OrganizationBrief getReferralDepartment(){
        OrganizationBrief o = new OrganizationBrief().setIdentifier(getIdentifier("KS001","2.16.156.10011.8"))
                .setClassCode(getCode("1","门诊","2.16.156.10011.81","科室分类"))
                .setName(new cn.org.chiss.rhin._2015.base.String().setValue("儿科"))
                .setPartOf(getPartOf());
        return o;
    }
    private List<Coding> getCode(String value,String display,String system,String systemName){
        List<Coding> coding = new ArrayList<>();
        coding.add(new Coding().setCode(new Code().setValue(value))
                .setDisplay(new cn.org.chiss.rhin._2015.base.String().setValue(display))
                .setSystem(new URI().setValue(system))
                .setSystemName(new cn.org.chiss.rhin._2015.base.String().setValue(systemName))
                .setVersion(new cn.org.chiss.rhin._2015.base.String().setValue("1.0")));
        return coding;
    }
    private PractitionerBrief getDoctor(){
        return new PractitionerBrief().setIdentifier(getIdentifier("0505015","2.16.156.10011.8"))
                .setName(new HumanName().setValue("杨瑞薛"))
                .setOrganization(new OrganizationBrief().setIdentifier(getIdentifier("2434","1")))
                .setDuty(getCode("","管理普通医师","",""))
                .setProfessionalTitle(getCode("","主任医师","",""));
    }
    private  List<Identifier> getIdentifier(String value, String system){
        List<Identifier> list = new ArrayList<>();
        list.add(new Identifier().setValue(new ID().setValue(value)).
                setSystem(new URI().setValue(system)));
        return list;
    }
    private PersonBrief getPatient(){
        return new PersonBrief().setIdentifier(getIdentifier("45032420001206611X","2.16.156.10011.1.3"))
                .setName(new HumanName().setValue("蒋科政"))
                .setBirthTime(new Date().setValue("20001206"))
                .setGender(new Code().setValue("1"))
                .setAddr(new Address().setText(new cn.org.chiss.rhin._2015.base.String().setValue("深圳市南山区南方科技大学宿舍欣园2栋")))
                .setTelecom(new Telecom().setValue(new cn.org.chiss.rhin._2015.base.String().setValue("13659631206")));
    }
    @Override
    public void scheduleDelete(ScheduleIDListType scheduleIDList) {

    }

    @Override
    public void scheduleFeed(ScheduleListType scheduleList) {

    }

    @Override
    public void reservationAbort(ReservationAbortParamType reservationAbortParam) {

    }
    private Period getValidPeriod(){
        return new Period().setStart(new DateTime().setValue("20231109153219"))
                .setEnd(new DateTime().setValue("20231109153219"))
                .setDuration(new Duration().setValue(new BigInteger("1")));
    }
    @Override
    @AuditLog
    public ScheduleQueryResponseType scheduleQuery(ScheduleQueryType scheduleQueryType) {
        if("1"
                .equals(scheduleQueryType.getAdhocQuery().getSlot().get(0).getValueList().getValue().get(0).getValue())){
            //模拟反例
            throw new RhinWSException("ReservationResponseFault","查询异常");
        }
        ScheduleQueryResponseType response  = new ScheduleQueryResponseType();
        ScheduleListType scheduleList = new ScheduleListType();
        List<ScheduleType> schedule = new ArrayList<>();
        ScheduleType s = new ScheduleType();
        s.setIdentifier(new Identifier().setValue(new ID().setValue("1")).
                        setSystem(new URI().setValue("2")));
        OrganizationBrief o = getReferralDepartment();
        o.getPartOf().getTelecom().setUse(new TelecomMode().setValue(TelecomModePrimitive.TELEPHONE));
        o.setName(new cn.org.chiss.rhin._2015.base.String().setValue("儿科"));
        s.setDepartment(o);
        Identifier identifier = new Identifier();
        identifier.setSystem(new URI().setValue("123"));
        identifier.setValue(new ID().setValue("01"));
        s.setDoctor(getDoctor().setIdentifier(getList(Identifier.class, identifier)));
        s.setAmpm(new Code().setValue("am"));
        s.setScheduletype(new Code().setValue("1"));
        s.setFee(new Quantity().setUnit(new cn.org.chiss.rhin._2015.base.String().setValue("元")).setValue(new Decimal().setValue(new BigDecimal(10).setScale(2, RoundingMode.DOWN))));
        s.setDate(new Date().setValue("20231003113000"));
        s.setAddress(new cn.org.chiss.rhin._2015.base.String().setValue("地址"));
        s.setStatus(new Code().setValue("0"));
        List<SliceType> slices = new ArrayList<>();
        SliceType slice = new SliceType();
        slice.setId(new ID().setValue("1")).setPeriod(getValidPeriod()).setClassCode(new Code().setValue("1"))
                        .setLimit(new Integer().setValue(100)).setUsed(new Integer().setValue(10));
        slices.add(slice);
        s.setSlice(slices);
        s.setLimit(new Integer().setValue(100)).setUsed(new Integer().setValue(10));
        schedule.add(s);
        scheduleList.setSchedule(schedule);
        response.setScheduleList(scheduleList);
        return response;
    }

    private <T>   List<T> getList(Class<T> c,T b){
        ArrayList<T> list = new ArrayList<T>();
        list.add(b);
        return list;
    }
}
